package com.houdoudou.affair.app;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.houdoudou.affair.jsp.Common;
import com.houdoudou.bean.AppBack;
import com.houdoudou.bean.BoxUsage;
import com.houdoudou.bean.Branch;
import com.houdoudou.bean.Customer;
import com.houdoudou.bean.Distribute;
import com.houdoudou.bean.Machine;
import com.houdoudou.bean.Order;
import com.houdoudou.bean.OrderProduct;
import com.houdoudou.bean.PasswordUtil;
import com.houdoudou.bean.Product;
import com.houdoudou.bean.Refund;
import com.houdoudou.bean.Sort;
import com.houdoudou.bean.Supplier;
import com.houdoudou.bean.TelnumVerify;
import com.houdoudou.bean.TransPasswordUtil;
import com.houdoudou.common.CommonJSON;
import com.houdoudou.common.CommonMathFunction;
import com.houdoudou.common.CommonSql;
import com.houdoudou.common.Constant;
import com.houdoudou.common.DateTimeFunction;
import com.houdoudou.dao.BoxUsageDao;
import com.houdoudou.dao.BranchDao;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.CustomerDao;
import com.houdoudou.dao.DistributeDao;
import com.houdoudou.dao.MachineDao;
import com.houdoudou.dao.OrderDao;
import com.houdoudou.dao.OrderProductDao;
import com.houdoudou.dao.OwnerDao;
import com.houdoudou.dao.ProductDao;
import com.houdoudou.dao.RefundDao;
import com.houdoudou.dao.SortDao;
import com.houdoudou.dao.SupplierDao;
import com.houdoudou.dao.TelnumVerifyDao;

public class CustomerAffairApp1 {

	
	/*------------------------------------------消费者搜索自提柜-------------------------------------------------*/
	
	public static void  searchMachine(HttpServletRequest req, HttpServletResponse resp){
		
		 double longitude = 0,latitude = 0;
		 Machine machine= new Machine();
		 JSONObject locationJO = null;
		JSONObject machineJO= new JSONObject();
		//获取Bean---------------------------	
		try{
		 String jsonData= req.getParameter("jsonData");
		  JSONObject jo= JSONObject.fromObject(jsonData);
	      locationJO= jo.getJSONObject("location");
	      machineJO= jo.getJSONObject("machine");
	      machine= (Machine) JSONObject.toBean(machineJO, Machine.class);
	      longitude = locationJO.getDouble("longitude");
		  latitude= locationJO.getDouble("latitude");
		 
		} catch(Exception e){
			new AppBack(req, resp, locationJO,false, "传入参数错误").send("location", "");
			return;
		}
	//	machine.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Machine> machines = new ArrayList<Machine>();
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();  
		   MachineDao machineDao= new  MachineDao(conn);
		   String sql=Machine.getSearchSql(machine);
		   System.out.println(sql);
		   machines= machineDao.search(sql);	
		   JSONArray  machinesJA=  JSONArray.fromObject(machines);
		   for(int i=machines.size()-1;i>=0;i--){
			   double distantce= CommonMathFunction.GetDistance(longitude, latitude, machines.get(i).getLongitude(),  machines.get(i).getLatitude());
		               machinesJA.getJSONObject(i).remove("secret");
		               boolean remove= (distantce>10000000)||machinesJA.getJSONObject(i).getString("timeCfg").equals("000");
			   if(remove){  //移除距离过大的自提柜  distantce>10000000
		    	   machinesJA.remove(i);
		       }else{
		    	   machinesJA.getJSONObject(i).put("distance", distantce);
		       }
		   }
		   machinesJA=CommonJSON.ascendJSONArray(machinesJA, "distance");
			new AppBack(req, resp, locationJO,machinesJA,true, "传入参数错误").send("location", "machines");
			return;
		}catch(Exception e){
			e.printStackTrace();
			new AppBack(req, resp,false,"数据库异常").send("", "");
		}finally{
			 ConnFactory.closeConn(conn);	
		}			 
			 
	}

	
/*------------------------------------------根据自提柜搜索商家-------------------------------------------------*/	
	public static void  searchSupplier(HttpServletRequest req, HttpServletResponse resp){
		
		 Machine machine= new Machine();
		 Distribute distribute= new Distribute();
		JSONObject machineJO= new JSONObject();
		
		//获取Bean---------------------------	
		try{
		 String jsonData= req.getParameter("jsonData");
		  JSONObject jo= JSONObject.fromObject(jsonData);
	      machineJO= jo.getJSONObject("machine");
	      machine= (Machine) JSONObject.toBean(machineJO, Machine.class); 	
	      
		} catch(Exception e){
			new AppBack(req, resp,false, "传入参数错误").send("", "");
			return;
		}
	//	machine.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Distribute> distributes = new ArrayList<Distribute>();
	//	 List<Supplier> suppliers = new ArrayList<Supplier>();
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();  
		   MachineDao machineDao= new  MachineDao(conn);
		   machine= machineDao.selectById(machine.getId());
		   if(machine.getTimeCfg().equals("222")){
			   new AppBack(req, resp, false, "自提柜暂停运营").send("", "suppliers");
			   return;
		   }
		   
		   JSONArray suppliersJA= new JSONArray();
		   
		   for(int x=1;x<4;x++){
			if(machine.getTimeCfg().substring(x-1,x).equals("1")){
			   distribute.setMachine(machine.getId());
			   distribute.setTimeFlag(x);
		   DistributeDao distributeDao= new  DistributeDao(conn);	  
		   distributes= distributeDao.search(DistributeDao.getSearchSql(distribute));	
		   //根据商家查询所有产品分配。
		   SupplierDao supplierDao= new SupplierDao(conn);
		   SortDao sortDao= new SortDao(conn);
		   ProductDao productDao= new ProductDao(conn);
		   BranchDao branchDao= new BranchDao(conn);		 
		   for(int i=0;i<distributes.size();i++){
			   Supplier supplier= new Supplier(); 		
			   supplier= supplierDao.selectById(distributes.get(i).getSupplier()); 
			   //屏蔽部分数据。
			 //  supplier.setRate(0);
			   supplier.setSecret("");
			  // supplier.setBoxFeeSu(0);
			   JSONObject supplierJO= JSONObject.fromObject(supplier);
			   int productNum=0;//产品数量
			   int saleNum=0; //销量
			   Sort  sort= new Sort();
			   sort.setSupplier(supplier.getTelnum());
			   List<Sort> sorts= sortDao.search(SortDao.getSearchSql(sort)); 
			   JSONArray sortsJA= new JSONArray();
			   for(int j=sorts.size()-1;j>=0;j--){			  
				   Product product= new Product();
				   product.setSupplier(supplier.getTelnum());
				   product.setSort(sorts.get(j).getId());
				   List<Product> products= productDao.search(ProductDao.getSearchSql(product));
				   productNum+= products.size();
				   JSONArray  productsJA= new JSONArray();
				   for(int k=products.size()-1;k>=0;k--){
					   saleNum+= products.get(k).getSale();
					   JSONObject productJO= JSONObject.fromObject(products.get(k));
					   Branch branch= new Branch();
					   branch.setProduct(products.get(k).getId());
					   branch.setIsAuth(1);
					   List<Branch> branches= branchDao.search(BranchDao.getSearchSql(branch));
					   if(branches.size()>0){
					   JSONArray branchesJA= JSONArray.fromObject(branches);
					   productJO.put("branches", branchesJA);
					   productsJA.add(productJO);}
				   }//产品
				   if(productsJA.size()>0){
				   JSONObject sortJO= JSONObject.fromObject(sorts.get(j));
				  sortJO.put("products", productsJA);
				  sortsJA.add(sortJO); }
			   }//分类
			   if(sortsJA.size()>0){
				 
			   supplierJO.put("sorts", sortsJA);
			   supplierJO.put("productNum", productNum);
			   supplierJO.put("saleNum", saleNum);	 
			   supplierJO.put("timeAA", distributes.get(i).getTimeAA());
			   supplierJO.put("timeFlag", x);
			   suppliersJA.add(supplierJO);
			   }
		   } //商家
		   }// 早中晚班中某一班启用
			}//早中晚循环	   
			new AppBack(req, resp, suppliersJA,true, "搜索成功").send("", "suppliers");
			return;
		}catch(Exception e){
			e.printStackTrace();
			new AppBack(req, resp,false,"数据库异常").send("", "");
		}finally{
			 ConnFactory.closeConn(conn);	
		}			 
			 
	}

/*------------------------------------------获取手机验证码-------------------------------------------------*/
	
	public static void createTelnumVerify(HttpServletRequest req, HttpServletResponse resp){
		
		TelnumVerify telnumVerify;
		JSONObject verifyJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 verifyJO= JSONObject.fromObject(jsonData);		 
		 telnumVerify= (TelnumVerify) JSONObject.toBean( verifyJO, TelnumVerify.class);	
		 if(telnumVerify.getTelnum().length()!=11){
			 new AppBack(req,resp, false, "电话位数不对").send("", "");
			    return ;
		 }
		 telnumVerify.setCode("1234");
		 telnumVerify.setValidTime(DateTimeFunction.getMyDateTime(10));
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   conn.setAutoCommit(false);
				   TelnumVerifyDao telnumVerifyDao= new TelnumVerifyDao(conn);	  
				   telnumVerifyDao.create(telnumVerify); 		 
				   conn.commit();
					  new AppBack(req,resp, true, "验证码发送成功").send("", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
		
	
/*------------------------------------------消费者注册-------------------------------------------------*/
	
	public static void createCustomer(HttpServletRequest req, HttpServletResponse resp){
		
		Customer customer; TelnumVerify telnumVerify;
		JSONObject customerJO,verifyJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 customerJO= paramJO.getJSONObject("customer");
		 verifyJO= paramJO.getJSONObject("verify");
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 customer.setSecret(TransPasswordUtil.decrypt(customer.getSecret()));
		 Customer.print("bean from app when createCustomer", customer);
		 
		 customer.setSecret(PasswordUtil.encrypt(customer.getSecret()));
		 customer.setMoney(0);
		 System.out.println("verifyJO"+verifyJO);
		 telnumVerify= ( TelnumVerify) JSONObject.toBean(verifyJO,  TelnumVerify.class);
		 telnumVerify.setValidTime(DateTimeFunction.getMyDateTime(0));
		 TelnumVerify.print("bean from app when createCustomer", telnumVerify);
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   conn.setAutoCommit(false);
				   TelnumVerifyDao telnumVerifyDao= new TelnumVerifyDao(conn);
				   if(!telnumVerifyDao.check(telnumVerify)){ new AppBack(req,resp, false, "验证码无效").send("", ""); return ;}				 
				   CustomerDao customerDao= new  CustomerDao(conn);	 			  
					customerDao.create(customer); 		 
					  conn.commit();
					  new AppBack(req,resp,JSONObject.fromObject(customer), true, "创建成功").send("customer", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
	
/*------------------------------------------更新支付密码-------------------------------------------------*/
	
	public static void updatePaySecret(HttpServletRequest req, HttpServletResponse resp){
		
		Customer customer; TelnumVerify telnumVerify;
		JSONObject customerJO,verifyJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 customerJO= paramJO.getJSONObject("customer");
		 verifyJO= paramJO.getJSONObject("verify");
		 
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 customer.setPaySecret(TransPasswordUtil.decrypt(customer.getPaySecret()));
		 Customer.print("bean from app when createCustomer", customer);	 
		 customer.setPaySecret(PasswordUtil.encrypt(customer.getPaySecret()));
		 
		 System.out.println("verifyJO"+verifyJO);
		 telnumVerify= ( TelnumVerify) JSONObject.toBean(verifyJO,  TelnumVerify.class);
		 telnumVerify.setValidTime(DateTimeFunction.getMyDateTime(0));
		 TelnumVerify.print("bean from app when createCustomer", telnumVerify);
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   if(! isCustomerValid(conn,customer)){new AppBack(req,resp, false, "消费者验证错误").send("", ""); return ;}				   
				   conn.setAutoCommit(false);
				   TelnumVerifyDao telnumVerifyDao= new TelnumVerifyDao(conn);
				   if(!telnumVerifyDao.check(telnumVerify)){ new AppBack(req,resp, false, "验证码无效").send("", ""); return ;}				 
				   CustomerDao customerDao= new  CustomerDao(conn);	 			  
					Customer customerExist= customerDao.selectById(customer.getTelnum());
					customerExist.setPaySecret(customer.getPaySecret());
					customerDao.update(customerExist);
					  conn.commit();
					  new AppBack(req,resp, true, "更新成功").send("", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
	
/*------------------------------------------更新支付密码-------------------------------------------------*/
	
	public static void updateSecret(HttpServletRequest req, HttpServletResponse resp){
		
		Customer customer; TelnumVerify telnumVerify;
		JSONObject customerJO,verifyJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 customerJO= paramJO.getJSONObject("customer");
		 verifyJO= paramJO.getJSONObject("verify");
		 
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 customer.setSecret(TransPasswordUtil.decrypt(customer.getSecret()));
		 Customer.print("bean from app when updateSecret", customer);	 
		 customer.setSecret(PasswordUtil.encrypt(customer.getSecret()));
		 
		 System.out.println("verifyJO"+verifyJO);
		 telnumVerify= ( TelnumVerify) JSONObject.toBean(verifyJO,  TelnumVerify.class);
		 telnumVerify.setValidTime(DateTimeFunction.getMyDateTime(0));
		 TelnumVerify.print("bean from app when updateSecret", telnumVerify);
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   
				   conn.setAutoCommit(false);
				   TelnumVerifyDao telnumVerifyDao= new TelnumVerifyDao(conn);
				   if(!telnumVerifyDao.check(telnumVerify)){ new AppBack(req,resp, false, "验证码无效").send("", ""); return ;}				 
				   CustomerDao customerDao= new  CustomerDao(conn);	 			  
					Customer customerExist= customerDao.selectById(customer.getTelnum());
					customerExist.setSecret(customer.getSecret());
					customerDao.update(customerExist);
					  conn.commit();
					  new AppBack(req,resp, true, "更新成功").send("", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
	
/*------------------------------------------消费者登陆-------------------------------------------------*/	
	
	public static void login(HttpServletRequest req, HttpServletResponse resp) throws IOException{		
	
		Customer customer = null;
		
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 JSONObject temp= JSONObject.fromObject(jsonData);
		 customer= (Customer) JSONObject.toBean(temp, Customer.class);
		 Customer.print("after customer app login ", customer);
		// customer= Customer.encrypt(customer);
		 if(customer.getTelnum().isEmpty()) { 
			new AppBack(req,resp, false, "传入参数不对").send("", "");
		    return;}
		 }catch(Exception e2){
			 e2.printStackTrace();	
			 new AppBack(req,resp, false, "传入参数不对").send("", "");
			    return ;
		 }
	
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		  
		   CustomerDao customerDao= new  CustomerDao(conn);	  
			Customer customerExist=  customerDao.selectById(customer.getTelnum());
			if(customerExist==null){
				new AppBack(req,resp, false, "用户不存在").send("", "");
				 return;
				
			}else{
				 customerExist= Customer.decrypt(customerExist);
				 customerExist= Customer.transEncrypt(customerExist);
				if(customerExist.getSecret().equals(customer.getSecret())){
				
					if(!customerExist.getPaySecret().isEmpty()){customerExist.setPaySecret("不为空");}
					JSONObject customerJO= JSONObject.fromObject(customerExist);
					new AppBack(req,resp, customerJO,true, "登陆成功").send("customer", "");
					 return;
				}else{
					new AppBack(req,resp, false, "密码错误").send("", "");
					 return;
				}		  
			}
		}catch(Exception e){
			e.printStackTrace();
			new AppBack(req,resp, false, "数据库异常").send("", "");
			 return;
			
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
/*------------------------------------------提交订单------------------------------------------------*/
	
	public static void createOrder(HttpServletRequest req, HttpServletResponse resp){
		
		Customer customer; Order order;
		List<OrderProduct> orderProducts;
		JSONObject customerJO,orderJO;
		JSONArray orderProductsJA;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		 orderProductsJA= (JSONArray) orderJO.remove("products");
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 order=(Order) JSONObject.toBean( orderJO, Order.class);
		 orderProducts= JSONArray.toList(orderProductsJA, OrderProduct.class);
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		 String sevralDaysBeforeDateTimeAA="";
		try {
			Date datimeAA= DateTimeFunction.parseMyDateFormat(order.getDateTimeAA());
			String datimeRDL= DateTimeFunction.myDateFormat(DateTimeFunction.addHour(datimeAA, Constant.hoursCustomerCanRefund));
			sevralDaysBeforeDateTimeAA=DateTimeFunction.myDateFormat(DateTimeFunction.addDay(datimeAA, Constant.daysSecretNoRepeate));
			order.setDateTimeRDL(datimeRDL);
		} catch (ParseException e2) {
			e2.printStackTrace();
			new AppBack(req,resp, false, "时间解析异常").send("", "");
		    return ;
		}
		
		order.setDateTimePL(DateTimeFunction.myDateFormat(new Date()));
		order.setState(2); //保护模式，因为提交成功后会马上进入支付界面。
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   conn.setAutoCommit(false);
				   OrderDao orderDao= new  OrderDao(conn);	
				   // 获取 空闲箱格
				   String boxesIn= new BoxUsageDao(conn).getIdleBox(order,2); //创建并未支付，对应箱格状态2
				   if(boxesIn==null){
					   new AppBack(req,resp, false, "没有多余箱格").send("", "");
					   return ;
				   }else{
					   order.setBoxesIn(boxesIn);
				   }
				   
				   // 设置 密码
				   boolean isExist=true;
				   String secret;
				   do{
				    secret= CommonMathFunction.genRandomNum(6);
				   String sql="select id from tb_order where secret="+secret+" and machineID="+order.getMachineID()+" and dateTimeAA > "+sevralDaysBeforeDateTimeAA;
				   System.out.println(sql);
				   isExist=orderDao.search(sql).size()>0;				   
				   }while(isExist);
				   order.setSecret(secret);
				   
				   
				 
				 double rateOw= new OwnerDao(conn).selectByTelnum(order.getOwnerTelnum()).getRate();
				 order.setRateOw(rateOw);				   
				   OrderProductDao  orderProductDao= new OrderProductDao(conn);
				  			  
					long orderID=orderDao.create(order); 
					order.setId(orderID);
					for(int i=0;i< orderProducts.size();i++){
						orderProducts.get(i).setOrderID(orderID);
						orderProductDao.create(orderProducts.get(i));
					}
					new BoxUsageDao(conn).updateOrderId(order);
					  conn.commit();
					  new AppBack(req,resp,JSONObject.fromObject(order) ,true, "创建成功").send("order", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback(); new BoxUsageDao(conn).updateBoxState(order,1); conn.commit();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
		
/*------------------------------------------用消费者账户余额支付订单------------------------------------------------*/
	
	public static void payOrder(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Order order;
		
		JSONObject customerJO,orderJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);	
		 
		 customer= Customer.transDecrypt(customer);
		 customer= Customer.encrypt(customer);
		 String paySecret= TransPasswordUtil.decrypt(customer.getPaySecret());
		 paySecret=PasswordUtil.encrypt(paySecret);
		 customer.setPaySecret(paySecret);
		 
		 order=(Order) JSONObject.toBean( orderJO, Order.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				   CustomerDao customerDao= new  CustomerDao(conn);	  
					Customer customerExist;	
					//验证用户是否合法，支付密码是否正确
					customerExist = customerDao.selectById(customer.getTelnum());
					if(customerExist==null){ new AppBack(req,resp, false, "用户不存在").send("", "");  return ;}
						
						if(!customerExist.getSecret().equals(customer.getSecret())){
							new AppBack(req,resp, false, "登陆密码不正确").send("", "");  return ;
						}
											
						if(!customerExist.getPaySecret().equals(customer.getPaySecret())){
							new AppBack(req,resp, false, "支付密码不正确").send("", "");  return ;
						}
						
						if(order.getMoneyCu()>customerExist.getMoney()){
							new AppBack(req,resp, false, "余额不足").send("", "");  return ;
						}
						customerExist.setMoney(customerExist.getMoney()-order.getMoneyCu());
						
					     customerDao.update(customerExist);
						//这里还需要记录消费者账户变动情况。
					  new AppBack(req,resp, true, "支付成功").send("", "");
				      return;
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}

/*------------------------------------------订单支付成功------------------------------------------------*/
	
	public static void makeOrderPaid(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Order order;
		
		JSONObject customerJO,orderJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 order=(Order) JSONObject.toBean( orderJO, Order.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();
				   conn.setAutoCommit(false);
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp, false, "消费者密码不整齐").send("", "");
				        return ;
				         }
			    OrderDao orderDao= new OrderDao(conn);
				Order orderExist= orderDao.selectById(order.getId());
			
				if(orderExist.getState()==1||orderExist.getState()==2){ //正常情况
					order.setState(10);		
					  order.setDateTimePA(DateTimeFunction.myDateFormat(new Date()));
					  order.setIsMoneyPaid(2);
					  orderDao.update(OrderDao.getUpdateSql(order));		
					  conn.commit();
					  new AppBack(req,resp,CommonSql.selectOrderRelated(conn, order.getId()), true, "支付成功").send("order", "");
					  return;
				  }else{
					  if(orderExist.getState()==3){ //
						  
						  //更新订单
						  order.setDateTimePA(DateTimeFunction.myDateFormat(new Date()));
						  order.setIsMoneyPaid(2);
						  order.setIsRefund(2);
						  orderDao.update(OrderDao.getUpdateSql(order));	
						  
						  // 订单已经被取消，退款给客户
						    CustomerDao customerDao= new CustomerDao(conn);
						    Customer customerExist= customerDao.selectById(customer.getTelnum());
						    customerExist.setMoney(customerExist.getMoney()+ orderExist.getMoneyCu());
						    customerDao.update(customerExist);	
						    
						 //  新建一张退款单。		
						    Refund refund= new Refund();
						    refund.setOrderID(orderExist.getId());
						    refund.setType(5);
						    refund.setResult(2);
						    refund.setState(12);
						    refund.setMoney(orderExist.getMoneyCu());
						    refund.setDateTimeCP(DateTimeFunction.getMyDateTime(0));
						    new RefundDao(conn). create(refund);
						    
						   //提交 
						    conn.commit();
						    
						    JSONObject result= new JSONObject();
							result.put("affair",  req.getParameter("affair"));
							result.put("success", true);
							result.put("errorInfo", "有问题");
							result.put("order", CommonSql.selectOrderRelated(conn, order.getId()));
							result.put("customer", JSONObject.fromObject(customerExist));
							
							try {	resp.getWriter().print(result.toString());} catch (IOException e) {	e.printStackTrace();}
					  }
				  }
				
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
/*------------------------------------------取消订单------------------------------------------------*/
	
	public static void cancelOrder(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Order order;
		
		JSONObject customerJO,orderJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 order=(Order) JSONObject.toBean( orderJO, Order.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();
				  
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp, false, "消费者密码不整齐").send("", "");
				        return ;
				         }
			    OrderDao orderDao= new OrderDao(conn);
				Order orderExist= orderDao.selectById(order.getId());
			
				if(orderExist.getState()!=1){ //正常情况	
					  new AppBack(req,resp,false, "订单，不能被取消").send("", "");
					  return;
				  }
			     if(orderExist.getIsMoneyPaid()==2){ 
			    	  new AppBack(req,resp,false, "订单已经被支付，不能被取消").send("", "");
					  return;
			     }
			   
						  //更新订单
			     conn.setAutoCommit(false); 
						  order.setDateTimeRF(DateTimeFunction.myDateFormat(new Date()));
						  order.setState(4);		 
						  orderDao.update(OrderDao.getUpdateSql(order));	
					conn.commit();	  
						
					 new AppBack(req,resp,CommonSql.selectOrderRelated(conn, order.getId()), true, "订单取消成功").send("order", "");
				
				
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
/*------------------------------------------拉取近一个月订单成功------------------------------------------------*/
	
	public static void pullLatestOrder(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; 
		
		JSONObject customerJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");	
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);			 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp, false, "消费者密码不正确").send("", "");
				        return ;
				         }
				   String lastMonth= DateTimeFunction.myDateFormat(DateTimeFunction.addMonth(new Date(), -1));
				  OrderDao orderDao= new OrderDao(conn);
				  OrderProductDao orderProductDao= new OrderProductDao(conn); 
				 
				  String sql= "select * from tb_order where customerTelnum="+ customer.getTelnum()+" and dateTimeAA >"+ lastMonth;
				  System.out.println(sql);
				  List<Order> orders= orderDao.search(sql);
				  JSONArray ordersJA=new  JSONArray();
				 
				  for(int i=0;i<orders.size();i++){
					  JSONObject orderJO= JSONObject.fromObject(orders.get(i));
					  
					  if(orders.get(i).getIsRefund()==2){
					  Refund refund=new RefundDao(conn).selectById(orders.get(i).getId());  
					  orderJO.put("refund", JSONObject.fromObject(refund));
					  }
				 List<OrderProduct> orderProducts= orderProductDao.selectById(orders.get(i).getId());				
				 orderJO.put("products", JSONArray.fromObject(orderProducts));
				 ordersJA.add(orderJO);
				  } 
				  new AppBack(req,resp, ordersJA, true, "查询成功").send("", "orders");
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
/*------------------------------------------订单支付保护------------------------------------------------*/
	
	public static void protectOrder(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Order order;
		
		JSONObject customerJO,orderJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 order=(Order) JSONObject.toBean( orderJO, Order.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp, false, "消费者密码不整齐").send("", "");
				        return ;
				         }
				  OrderDao orderDao= new OrderDao(conn);
				Order orderExist= orderDao.selectById(order.getId());
				if(orderExist.getState()==1||orderExist.getState()==2&&orderExist.getIsMoneyPaid()==1){
					order.setState(2);
					orderExist.setState(2);
					  orderDao.update(OrderDao.getUpdateSql(order));
					  new AppBack(req,resp,  JSONObject.fromObject(orderExist),true, "保护成功").send("order", ""); return;
					}else { String backerror; 
						if(orderExist.getIsMoneyPaid()==2){ backerror="已完成支付";
						}else{  backerror="订单已被取消";}
						
						 new AppBack(req,resp, JSONObject.fromObject(orderExist), false, backerror).send("order", ""); return;
					}
				
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
	
	
/*------------------------------------------解除订单支付保护------------------------------------------------*/
	
	public static void deProtectOrder(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Order order;
		
		JSONObject customerJO,orderJO;
		//获取Bean---------------------------
		try{
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 orderJO= paramJO.getJSONObject("order");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
		 order=(Order) JSONObject.toBean( orderJO, Order.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp, false, "数据解析错误").send("", "");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	
				try{
				   conn= ConnFactory.getConn();
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp, false, "消费者密码不整齐").send("", "");
				        return ;
				         }
				  OrderDao orderDao= new OrderDao(conn);
				Order orderExist= orderDao.selectById(order.getId());
				if(orderExist.getState()==1||orderExist.getState()==2&&orderExist.getIsMoneyPaid()==1){
					order.setState(1);
					orderExist.setState(1);
					  orderDao.update(OrderDao.getUpdateSql(order));
					  new AppBack(req,resp,  JSONObject.fromObject(orderExist),true, "保护成功").send("order", ""); return;
					}else { String backerror; 
						if(orderExist.getIsMoneyPaid()==2){ backerror="已完成支付";
						}else{  backerror="订单状态异常";}
						
						 new AppBack(req,resp, JSONObject.fromObject(orderExist), false, backerror).send("order", ""); return;
					}
				
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp, false, "数据库异常").send("", "");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}

	
	
	
/*------------------------------------------创建退款------------------------------------------------*/
	
	public static void createRefund(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Refund refund;
		JSONObject backJO= new JSONObject();
		
		//获取Bean---------------------------
		try{
			JSONObject customerJO,refundJO; 
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 refundJO= paramJO.getJSONObject("refund");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
	    	refund= (Refund) JSONObject.toBean( refundJO, Refund.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp).send(1,  "数据解析错误");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();
				 
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp).send(1, "客户验证失败");
				        return ;
				         }
			    OrderDao orderDao= new OrderDao(conn);
			    
				Order orderExist= orderDao.selectById(refund.getOrderID());
			      long dateTimeRDL= Long.parseLong(orderExist.getDateTimeRDL());
			      backJO.put("order", CommonSql.selectOrderRelated(conn, orderExist.getId()));
			      if(DateTimeFunction.myDateTimeValue(new Date())>dateTimeRDL){		    	 
			    		new AppBack(req,resp,backJO).send(3,"已过最迟退款时间"); return;
			      } 
			      if(orderExist.getIsMoneyPaid()!=2){
			    	  new AppBack(req,resp,backJO).send(3,"不可退款"); return;
			      }
			      
			   refund.setResult(1); refund.setState(1);
			   refund.setDateTimeCP(DateTimeFunction.myDateFormat(new Date()));			   
			   Date dateTimeSL= DateTimeFunction.addHour(new Date(),Constant.hoursBeforSupplierDealRefund);
				refund.setDateTimeSL(DateTimeFunction.myDateFormat(dateTimeSL));	   
			   conn.setAutoCommit(false);
			   new RefundDao(conn).create(refund);
			   
			   Order orderUpdate= new Order();
			   orderUpdate.setId(refund.getOrderID());
			   orderUpdate.setIsRefund(2);
			   orderDao.update(OrderDao.getUpdateSql(orderUpdate));
			   
			   conn.commit();
			   backJO.put("order", CommonSql.selectOrderRelated(conn, orderExist.getId()));
				new AppBack(req,resp,backJO ).send(2,"申请退款成功");
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp).send(1, "数据库异常");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}

/*------------------------------------------修改退款------------------------------------------------*/
	
	public static void updateRefund(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Refund refund;
		JSONObject backJO= new JSONObject();
		
		//获取Bean---------------------------
		try{
			JSONObject customerJO,refundJO; 
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 refundJO= paramJO.getJSONObject("refund");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
	    	refund= (Refund) JSONObject.toBean( refundJO, Refund.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp).send(1, "数据解析错误");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();			 
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp).send(1, "客户验证失败");
				        return ;
				         }	
				RefundDao refundDao= new RefundDao(conn);   
				Refund refundExist= refundDao.selectById(refund.getOrderID());
				     if(refundExist.getResult()==2||refundExist.getResult()==3){
				    	 backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
				    	 new AppBack(req,resp,backJO ).send(3,"退款已经结束，不能修改");  return;
				     }			
				     
			   conn.setAutoCommit(false);
			   refundDao.update(refund);
			   conn.commit();
			   backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
				new AppBack(req,resp,backJO ).send(2,"修改退款成功");
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp).send(1, "数据库异常");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}
	
/*------------------------------------------取消退款------------------------------------------------*/
	
	public static void cancelRefund(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Refund refund;
		JSONObject backJO= new JSONObject();
		
		//获取Bean---------------------------
		try{
			JSONObject customerJO,refundJO; 
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 refundJO= paramJO.getJSONObject("refund");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
	    	refund= (Refund) JSONObject.toBean( refundJO, Refund.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp).send(1, "数据解析错误");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();			 
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp).send(1, "客户验证失败");
				        return ;
				         }	
				       RefundDao refundDao= new RefundDao(conn);   
						Refund refundExist= refundDao.selectById(refund.getOrderID());
						if(refundExist.getResult()==2||refundExist.getResult()==3){
							 backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
					    	 new AppBack(req,resp,backJO ).send(3,"退款已经结束");  return;
					     }
				Refund refundUpdate= new Refund();
				refundUpdate.setOrderID(refund.getOrderID());
				refundUpdate.setState(10);
				refundUpdate.setDateTimeCC(DateTimeFunction.myDateFormat(new Date()));
				refundUpdate.setResult(3);
			   conn.setAutoCommit(false);
			   new RefundDao(conn).update(refundUpdate);
			   conn.commit();
			   backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
				new AppBack(req,resp,backJO).send(2,"取消退款成功");
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp).send(1, "数据库异常");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}	
/*------------------------------------------取消退款------------------------------------------------*/
	
	public static void arbitrateRefund(HttpServletRequest req, HttpServletResponse resp){
		Customer customer; Refund refund;
		JSONObject backJO= new JSONObject();
		
		//获取Bean---------------------------
		try{
			JSONObject customerJO,refundJO; 
		 String jsonData= req.getParameter("jsonData");
		 System.out.println(jsonData);
		 JSONObject paramJO=JSONObject.fromObject(jsonData);
		 
		 customerJO= paramJO.getJSONObject("customer");
		 refundJO= paramJO.getJSONObject("refund");
		
		 customer= (Customer) JSONObject.toBean( customerJO, Customer.class);		
	    	refund= (Refund) JSONObject.toBean( refundJO, Refund.class);	
		 
		 }catch(Exception e2){
			 e2.printStackTrace();			
				new AppBack(req,resp).send(1, "数据解析错误");
			    return ;
		 }
		
		//调用 dao处理事务 -------------------------
				Connection conn = null;	 
				try{
				   conn= ConnFactory.getConn();			 
				       if(! isCustomerValid(conn, customer)){
				      	new AppBack(req,resp).send(1, "客户验证失败");
				        return ;
				         }	
				       RefundDao refundDao= new RefundDao(conn);   
						Refund refundExist= refundDao.selectById(refund.getOrderID());
						boolean isCouldDo=true;
						long dateTimeCL= Long.parseLong(refundExist.getDateTimeCL());
						isCouldDo=isCouldDo&&(dateTimeCL< DateTimeFunction.myDateTimeValue(new Date()));
						isCouldDo=isCouldDo&&(refundExist.getResult()==2);
						isCouldDo=isCouldDo&&(refundExist.getState()==4);
						
						if(!isCouldDo){
							 backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
					    	 new AppBack(req,resp,backJO ).send(3,"不满足仲裁条件"); 
					    	 return;
					     }     
				Refund refundUpdate= new Refund();
				refundUpdate.setState(6);
				refundUpdate.setDateTimeC(DateTimeFunction.myDateFormat(new Date()));
				Date dateTimeOL= DateTimeFunction.addHour(new Date(),Constant.hoursBeforOwnerDealRefund);
				refundUpdate.setDateTimeOL(DateTimeFunction.myDateFormat(dateTimeOL));
				refundUpdate.setResult(-1);
			   conn.setAutoCommit(false);
			   refundDao.update(refundUpdate);
			   conn.commit();
			   backJO.put("order", CommonSql.selectOrderRelated(conn,refund.getOrderID()));
				new AppBack(req,resp,backJO).send(2,"取消退款成功");
				}catch(Exception e){
					e.printStackTrace();
					try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
					new AppBack(req,resp).send(1, "数据库异常");
				}finally{
					 ConnFactory.closeConn(conn);					
				}
	}	
	
	
	
	/*------------------------------------------验证消费者------------------------------------------------*/	
	private static boolean isCustomerValid(Connection conn,Customer customer){
		boolean result=true;
		 CustomerDao customerDao= new  CustomerDao(conn);	  
			Customer customerExist;
			try {
				customerExist = customerDao.selectById(customer.getTelnum());
			if(customerExist==null){ return false;}
				 customerExist= Customer.decrypt(customerExist);
				 customerExist= Customer.transEncrypt(customerExist);
				if(customerExist.getSecret().equals(customer.getSecret())){
					 return true;
				}else{	 return false;}	
				 }
			 catch (Exception e) {
				e.printStackTrace(); return false;
			}
		
	}
	
}
